package com.frank.oj.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.dto.CompileDTO;
import com.frank.model.entity.problem.Problem;
import com.frank.model.entity.problem.ProblemCase;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.ProblemDTO;
import com.frank.oj.service.admin.problem.AdminProblemService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/2
 */
@RestController
@RequestMapping("/api/admin/problem")
@RequiresAuthentication
@RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
public class AdminProblemController {
    @Resource
    private AdminProblemService adminProblemService;

    /**
     * 获取题目列表
     */
    @GetMapping("/get-problem-list")
    public CommonResult<IPage<Problem>> getProblemList(@RequestParam(value = "limit", required = false) Integer limit,
                                                       @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                       @RequestParam(value = "keyword", required = false) String keyword,
                                                       @RequestParam(value = "auth", required = false) Integer auth,
                                                       @RequestParam(value = "oj", required = false) String oj){
        return CommonResult.successResponse(adminProblemService.getProblemList(limit, currentPage, keyword, auth, oj));
    }

    /**
     * 根据id查询题目信息
     */
    @GetMapping()
    public CommonResult<Problem> getProblemById(@RequestParam("pid") Long pid){
        return adminProblemService.getProblemById(pid);
    }

    /**
     * 测评数据
     * @param pid 题目id
     * @param isUpload 上传文件/手动输入
     * @return 测评信息
     */
    @GetMapping("/get-problem-cases")
    public CommonResult<List<ProblemCase>> getProblemCases(@RequestParam("pid") Long pid,
                                                           @RequestParam(value = "isUpload", defaultValue = "true") Boolean isUpload) {
        return adminProblemService.getProblemCases(pid, isUpload);
    }

    /**
     * 手动添加题目
     * @param problemDTO 题目信息
     * @return null
     */
    @PostMapping()
    public CommonResult<Void> addProblem(@RequestBody ProblemDTO problemDTO){
        return adminProblemService.addProblem(problemDTO);
    }

    /**
     * 导入远程测评题目
     * @param name 平台
     * @param problemId 题目id
     * @return null
     */
    @GetMapping("/import-remote-oj-problem")
    public CommonResult<Void> importRemoteOJProblem(@RequestParam("name") String name,
                                                    @RequestParam("problemId") String problemId){

        return adminProblemService.importRemoteOJProblem(name, problemId);
    }

    /**
     * 修改题目
     * @param problemDTO 题目信息
     * @return null
     */
    @PutMapping()
    public CommonResult<Void> updateProblem(@RequestBody ProblemDTO problemDTO){
        return adminProblemService.updateProblem(problemDTO);
    }

    /**
     * 删除题目
     * @param pid 题目id
     * @return null
     */
    @DeleteMapping()
    public CommonResult<Void> deleteProblem(@RequestParam("pid") Long pid){
        return adminProblemService.deleteProblem(pid);
    }
    @PutMapping("/change-problem-auth")
    public CommonResult<Void> changeProblemAuth(@RequestBody Problem problem) {
        return adminProblemService.changeProblemAuth(problem);
    }

    @PostMapping("/compile-spj")
    public CommonResult compileSpj(@RequestBody CompileDTO compileDTO) {
        return adminProblemService.compileSpj(compileDTO);
    }

    @PostMapping("/compile-interactive")
    public CommonResult compileInteractive(@RequestBody CompileDTO compileDTO) {
        return adminProblemService.compileInteractive(compileDTO);
    }

    @DeleteMapping("delete-problems")
    public CommonResult<Void> deleteProblems(@RequestParam("params") List<Long> pids) {
        return adminProblemService.deleteProblems(pids);
    }
}
